<?php

/**
 * @Author: chenjianghui
 * @Date:   2018-08-02 11:40:50
 * @Last Modified by:   chenjianghui
 * @Last Modified time: 2018-10-12 17:48:07
 */
namespace app\common\queue;

use app\common\model\Message;
use app\common\model\Queue;
use \think\queue\Job;
use \think\Db;
use app\admin\model\shop\Order as shopOrder;


class CancelOrderQueueClient
{


    /**
     * 站内信
     * @Author   chenjianghui
     * @DateTime 2018-08-02T14:03:50+0800
     * @param    Job                      $job  [description]
     * @param    [type]                   $data [description]
     * @return   [type]                         [description]
     */
    public function instationMsg(Job $job, $data)
    {

        //print("<info>开始执行任务: ".var_export($data,true)."</info> \n");
        $isJobDone = $this->send_instationMsg($data);
        //dump($isJobDone);die;
        if ($isJobDone) {
            //成功删除任务
            $job->delete();
            //echo '执行任务成功:删除任务'.'</br>';
        } else {
            //任务轮询4次后删除
            if ($job->attempts() > 3) {
                // 第1种处理方式：重新发布任务,该任务延迟10秒后再执行
                //$job->release(10);
                // 第2种处理方式：原任务的基础上1分钟执行一次并增加尝试次数
                //$job->failed();
                // 第3种处理方式：删除任务
                $job->delete();
            }
        }
    }

    public function send_instationMsg($data)
    {

        $filename = ROOT_PATH . '/log/order_cancle/' . date('Ymd') . '.log';
        $shop_order_info = \think\Db::name("shop_order")->where("order_id",$data["message_send_id"])->find();
        $shop_order = new shopOrder();
        $shop_order->order_cancle_func($data["message_send_id"],2);

            $op = \think\Db::name("queue")->where("id",$data["id"])->update(["status"=>1]);
            if($op){

                $str = PHP_EOL.date('Y-m-d H:i:s', time()).'--gere订单编号:'.$shop_order_info["order_sno"].'que_id='.$data['id'].', 用户id = ' .$shop_order_info['uid'] .', res = succeess('. json_encode($shop_order_info) .')'.PHP_EOL;
                file_put_contents($filename, $str . PHP_EOL, FILE_APPEND);

            } else {

                $str = PHP_EOL.date('Y-m-d H:i:s', time()).'--gere订单编号:'.$shop_order_info["order_sno"].'que_id='.$data['id'].', 用户id = ' .$shop_order_info['uid'] .', res = fail('. json_encode($shop_order_info) .')'.PHP_EOL;
                file_put_contents($filename, $str . PHP_EOL, FILE_APPEND);

            }
    }



}
